<?php

require_once('../includes/DB.class.php');
require_once('../includes/SmartyManager.class.php');
require_once('../includes/Filter.class.php');

$smarty = SmartyManager::getSmarty();
try{
	$keyname = Filter::forDBInsertion($_GET['keyname']);
	
	$smarty->assign('keyname', $_GET['keyname']);
	$smarty->assign('haveOrderNumber', true);
	$query = "select * from little_main where certificationNumber = '{$keyname}' order by size1, size2, size3";
	DB::query($query);
	
	if(DB::num_rows() > 0){
		
	$id = array();
	
	$certificationNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$weight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$facheNumber = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
	$result = DB::getResult();
	
	while($row = $result->fetch_assoc()){
		array_push($id, $row['id']);
		array_push($certificationNumber, $row['certificationNumber']);
		array_push($certificationCodification, $row['certificationCodification']);
		array_push($sequenceNumber, $row['sequenceNumber']);
		array_push($manufactory, $row['manufactory']);
		array_push($authenticationMark, $row['authenticationMark']);
		array_push($steelNumber, $row['steelNumber']);	
		array_push($size1, $row['size1']);
		array_push($size2, $row['size2']);
		array_push($size3, $row['size3']);
		array_push($unitWeight, $row['unitWeight']);	
		array_push($count, $row['count']);
		array_push($weight, $row['unitWeight']*$row['count']);
		array_push($orderNumber, $row['orderNumber']);
		array_push($orderSubitemNumber, $row['orderSubitemNumber']);
		array_push($unitPrice,$row['unitPrice']);
		array_push($materialNumber, $row['materialNumber']);
		array_push($consignmentBatch, $row['consignmentBatch']);
		array_push($sampleBatch,$row['sampleBatch']);
		array_push($facheNumber,$row['facheNumber']);
		array_push($purchaseNumber,$row['purchaseNumber']);
		array_push($shipmentDate,$row['shipmentDate']);
		array_push($month,$row['month']);
		array_push($vehicleNumber, $row['vehicleNumber']);
		array_push($consignee, $row['consignee']);
		array_push($remark, $row['remark']);
		array_push($filename, $row['filename']);
		array_push($uploadTime, $row['uploadTime']);
	}
		
	$smarty->assign(array(
			'main_id' => $id,
			'main_certificationNumber'	=> 	$certificationNumber,
			'main_certificationCodification'		=> 	$certificationCodification,
			'main_sequenceNumber'		=> 	$sequenceNumber,
			'main_manufactory'		=> 	$manufactory,
			'main_authenticationMark'		=> 	$authenticationMark,
			'main_steelNumber'		=> 	$steelNumber,
			'main_size1'		=> 	$size1,
			'main_size2'		=> 	$size2,
			'main_size3'		=> 	$size3,
			'main_count'	=>	$count,
			'main_unitWeight'		=>	$unitWeight,
			'main_weight'	=>	$weight,
			'main_orderNumber'		=>	$orderNumber,
			'main_orderSubitemNumber'	=>	$orderSubitemNumber,
			'main_unitPrice'		=>	$unitPrice,
			'main_materialNumber'		=>  $materialNumber,
			'main_consignmentBatch'	=>  $consignmentBatch,
			'main_sampleBatch'		=>  $sampleBatch,
			'main_facheNumber'	=>  $facheNumber,
			'main_purchaseNumber'	=>  $purchaseNumber,
			'main_shipmentDate'	=>  $shipmentDate,
			'main_month'	=>  $month,
			'main_vehicleNumber'	=>  $vehicleNumber,
			'main_consignee'	=>  $consignee,
			'main_remark'	=>  $remark,
			'main_filename'	=>  $filename,
			'main_uploadTime'	=>  $uploadTime
			));
		$planSumCount = 0;
		$planSumWeight = 0;
		foreach($count as $c){
			$planSumCount += intval($c);
		}
		foreach($weight as $w){
			$planSumWeight += $w;
		}
		
		$smarty->assign('planSumCount', $planSumCount);
		$smarty->assign('planSumWeight', $planSumWeight);
		$smarty->assign('planRowspan', DB::num_rows() + 2);
	}
	
	//然后是对于入库的查询，也是简简单单地只需要查询那些phase为"入库"的发车的项目，然后进行小结
	$query = "select * from little_ruku where certificationNumber = '{$keyname}' order by certificationNumber";
	DB::query($query);
	
	if(DB::num_rows() > 0){
		$id = array();
		$rukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		$result = DB::getResult();

		while($row = $result->fetch_assoc()){
			array_push($id,$row['id']);
			array_push($rukuDate, $row['rukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array(
					'ruku_id' => $id,	
					'ruku_rukuDate' => $rukuDate,
					'ruku_certificationNumber' => $certificationNumber,
					'ruku_certificationCodification' => $certificationCodification,
					'ruku_consignmentBatch' => $consignmentBatch,
					'ruku_remark1' => $remark1,
					'ruku_remark2' => $remark2,
					'ruku_filename' => $filename,
					'ruku_uploadTime' => $uploadTime
				));
	}
	//对于出库的， 需要查询发船中的所有和发车中的标志为“出库”的信息，然后进行相加求出小结
	$query = "select * from 
				(
					(select *,id as chukuId from little_chuku where certificationNumber = '$keyname') as chukuTable
				  		left join
				  	(select id,filename,uploadTime from little_chuku_file) as chukuFileTable
				  	on (chukuFileTable.id = chukuTable.fileId)
				 )
				 order by certificationNumber";
	
	DB::query($query);
	
	if(DB::num_rows() > 0){
		$id = array();
		$chukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$handler = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($id,$row['chukuId']);
			array_push($chukuDate, $row['chukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($handler, $row['handler']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array(
					'chuku_id' => $id,
					'chuku_chukuDate' => $chukuDate,
					'chuku_certificationNumber' => $certificationNumber,
					'chuku_certificationCodification' => $certificationCodification,
					'chuku_handler' => $handler,
					'chuku_consignmentBatch' => $consignmentBatch,
					'chuku_remark1' => $remark1,
					'chuku_remark2' => $remark2,
					'chuku_filename' => $filename,
					'chuku_uploadTime' => $uploadTime
				));
	
	}
	$smarty->display('multipleTable.html');
}catch(Exception $e){
	$smarty = SmartyManager::getSmarty();
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}

?>